package com.app.qbake.module.mine;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.repo.ConfigRepo;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.ViewInflater;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.WalletRequest;
import com.wind.data.mine.WalletResponse;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.WithdrawMoneyActivity;
import com.app.qbake.module.mine.di.WalletComponent;
import com.app.qbake.module.mine.event.WithdrawMoneySuccessEvent;
import com.app.qbake.module.mine.mvp.WalletView;
import com.app.qbake.module.mine.presenter.WalletPresenter;
import com.wind.toastlib.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.WalletView",
                responseCanonicalName = "com.wind.data.mine.WalletResponse",
                requestCanonicalName = "com.wind.data.mine.WalletRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "user/account")
)
public class WalletFragment extends BaseLoadingFragment<WalletView, WalletPresenter, WalletComponent, WalletResponse>
        implements WalletView {
    @BindView(R.id.tv_account)
    TextView tv_account;

    @BindView(R.id.tv_amount)
    TextView tv_amount;

    @BindView(R.id.account_cash)
    TextView account_cash;

    @BindView(R.id.account_frozen)
    TextView account_frozen;

    @BindView(R.id.tv_fee)
    TextView tv_fee;
    @Inject
    WalletPresenter presenter;
    private WalletResponse mWalletResponse;

    @Override
    public WalletPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected BaseRequest buildLoadingRequest() {
        WalletRequest request = new WalletRequest();
        return request;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_wallet;
    }

    @Override
    public void showPage(WalletResponse reponse) {
        mWalletResponse = reponse;
        ViewInflater.inflateText(tv_account, reponse.getData().getAccount());
        ViewInflater.inflateText(tv_amount, reponse.getData().getAmount());
        ViewInflater.inflateText(account_cash, reponse.getData().getAccount_cash());
        ViewInflater.inflateText(account_frozen, reponse.getData().getAccount_frozen());
        ViewInflater.inflateText(tv_fee, ConfigRepo.getInstance().get(getActivity()).getCost_platform());

        mLayoutManager.showContent();

    }

    @OnClick(R.id.tv_withdraw_money)
    public void onViewClick(View view) {
        //判断是否有可提现金额
        if (mWalletResponse != null) {
            double amount = 0;
            try {
                amount = Double.parseDouble(mWalletResponse.getData().getAmount());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (amount > 0)
                if (amount>=100) {
                    WithdrawMoneyActivity.start(getActivity(), amount);
                }else {
                    ToastUtil.showToast(getActivity(), "可提现金额必须大于等于100元哦");
                }
            else {
                ToastUtil.showToast(getActivity(), "没有可提现金额");
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void onWithdrawMoneySuccessEvent(WithdrawMoneySuccessEvent event){
        ActivityUtil.finish(getActivity());
    }
}
